package network;
// 규석아~~ 형마음 알지~? ㅋㅋ

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.JList;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

//import net.miginfocom.swing.MigLayout;




import javax.swing.JMenuBar;

import java.awt.GridLayout;

//import com.jgoodies.forms.layout.FormLayout;
//import com.jgoodies.forms.layout.ColumnSpec;
//import com.jgoodies.forms.layout.RowSpec;

import java.awt.Button;

import javax.swing.DefaultComboBoxModel;

import java.awt.Font;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Vector;

public class MultiChatClient extends JFrame implements Runnable {
	InetAddress ia;
	Socket socket;
	int port = 4444;
	MultiChatClientThread mcct;
	StringBuilder data=new StringBuilder();
	Thread t;
	Vector<String> clientList = new Vector<>();
	
	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JButton btnNewButton_2;
	JTextField textField;
	private JTextPane result;
	private JComboBox comboBox;
	private JScrollPane scrollPane_1;
	JList list;
	private JPanel panel_3;
	private JMenuBar menuBar;
	private JPanel panel_4;
	private JButton btnNewButton_3;
	private JLabel label;
	private JPanel panel_5;
	private JLabel label_1;
	private JTextField hostAddress;
	private JLabel label_2;
	JTextField userID;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MultiChatClient frame = new MultiChatClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MultiChatClient() {
		try {
			ia = InetAddress.getLocalHost();
			setTitle(ia + " || 포트 : " + port);
		} catch (Exception e) {
			e.printStackTrace();
		}
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				serverDisconnect();
			}
		});
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 699, 439);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBackground(new Color(152, 251, 152));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		contentPane.add(getPanel_2(), BorderLayout.WEST);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		contentPane.add(getPanel_3(), BorderLayout.EAST);
	}

	void serverConnect() throws Exception{
		t = new Thread(this);
		t.start();
	}
	
	void serverDisconnect() {
		t.interrupt(); 
	}
	
	@Override
	public void run() {
		try {
			socket = new Socket(ia.getHostAddress(), port);
			mcct = new MultiChatClientThread(this, socket);
			mcct.start(); //여기나 mcct의 생성자나 둘 중 한군데에서
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	void sendMsg() {
		SocketData sd = new SocketData("message", userID.getText(), textField.getText(), null);
		mcct.sendMsg(sd);
		dataSet(sd);
	}
	
	public void dataSet(SocketData ds){
//		System.out.println(ds.getUser()); // 여기 한 군데만 넣으면 양쪽 다 뜸?
		data.append(ds.getUser()+":"+ds.getMessage()+"<br/>"); 
		result.setText(data.toString());
		result.setCaretPosition(result.getDocument().getLength());
	}
		
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getBtnNewButton_2(), BorderLayout.WEST);
			panel.add(getPanel_5(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getBtnNewButton_1(), BorderLayout.EAST);
			panel_1.add(getTextField(), BorderLayout.CENTER);
			panel_1.add(getComboBox(), BorderLayout.WEST);
		}
		return panel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBackground(Color.WHITE);
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getScrollPane_1(), BorderLayout.CENTER);
			panel_2.add(getBtnNewButton_3(), BorderLayout.SOUTH);
		}
		return panel_2;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("그만두기");
			btnNewButton.setEnabled(false);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						btnNewButton.setEnabled(false);
						btnNewButton_1.setEnabled(false);
						btnNewButton_2.setEnabled(true);
						serverDisconnect();				
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("전송");
			btnNewButton_1.setEnabled(false);
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						sendMsg();						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("접속");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						btnNewButton.setEnabled(true);
						btnNewButton_1.setEnabled(true);
						btnNewButton_2.setEnabled(false);
						serverConnect();						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		return btnNewButton_2;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent arg0) {
					if (arg0.getKeyCode()==KeyEvent.VK_ENTER) sendMsg();						
				}
			});
			textField.setColumns(10);
		}
		return textField;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"이모티콘"}));
		}
		return comboBox;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setViewportView(getList());
			scrollPane_1.setColumnHeaderView(getLabel());
		}
		return scrollPane_1;
	}
	private JList getList() {
		if (list == null) {
			list = new JList();
		}
		return list;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new BorderLayout(0, 0));
			panel_3.add(getPanel_4(), BorderLayout.CENTER);
		}
		return panel_3;
	}
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
		}
		return menuBar;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
		}
		return panel_4;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("귓속말 해제");
		}
		return btnNewButton_3;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("현재 접속자");
			label.setFont(new Font("굴림", Font.BOLD, 16));
			label.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return label;
	}
	private JPanel getPanel_5() {
		if (panel_5 == null) {
			panel_5 = new JPanel();
			panel_5.add(getLabel_1());
			panel_5.add(getTextField_1_1());
			panel_5.add(getLabel_2());
			panel_5.add(getUserId());
		}
		return panel_5;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("서버주소");
		}
		return label_1;
	}
	private JTextField getTextField_1_1() {
		if (hostAddress == null) {
			hostAddress = new JTextField();
			hostAddress.setText("192.168.10.101");
			hostAddress.setColumns(10);
		}
		return hostAddress;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("아이디");
		}
		return label_2;
	}
	private JTextField getUserId() {
		if (userID == null) {
			userID = new JTextField();
			userID.setText("최규석");
			userID.setColumns(10);
		}
		return userID;
	}
}
